package com.cheng.operation.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.IdUtil;
import com.cheng.common.utils.RespUtils;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.AcceptInfoEntity;
import com.cheng.data.entity.CompanyBaseInfoEntity;
import com.cheng.data.entity.CompanyInfoEntity;
import com.cheng.data.vo.*;
import com.cheng.operation.*;
import com.cheng.service.AcceptInfoService;
import com.cheng.service.CompanyBaseInfoService;
import com.cheng.service.CompanyInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author: ChengJW
 * @date: 2021/11/8 20:57
 * @version: 1.0
 * @description: 受理信息表 业务逻辑处理
 */
@Service
public class AcceptInfoOperationImpl implements AcceptInfoOperation {

    private final static Logger logger = LoggerFactory.getLogger(AcceptInfoOperationImpl.class);

    @Autowired
    private AcceptInfoService acceptInfoService;

    @Autowired
    private CompanyBaseInfoOperation companyBaseInfoOperation;

    @Autowired
    private CompanyBaseInfoService companyBaseInfoService;

    @Autowired
    private AcptTypeInfOperation acptTypeInfOperation;

    @Autowired
    private ArchiveTypeInfOperation archiveTypeInfOperation;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private GvmtTypeInfOperation gvmtTypeInfOperation;


    /**
     * @author: ChengJW
     * @params: [atAcptId]
     * @return: int
     * @date: 2021/9/20
     * @description: 插入 acceptInfo 信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertAcceptInfo(String atAcptId) throws CommonException {

        //根据受理号查询受理信息
        AcceptInfoEntity acceptEntity = new AcceptInfoEntity();
        acceptEntity.setAtAcptId(atAcptId);
        List<AcceptInfoEntity> list = acceptInfoService.selectAcceptInfo(acceptEntity);
        if (null != list && list.size() > 0){
            logger.error("该企业已经完成了登记");
            throw new CommonException("CJW202111010028","AcceptInfoOperationImpl","insertAcceptInfo");
        }
        //根据受理号查询 companyBaseInfo 信息
        CompanyBaseInfoEntity entity = new CompanyBaseInfoEntity();
        entity.setCyAcptId(atAcptId);
        CompanyBaseInfoVO companyBaseInfoVO = companyBaseInfoOperation.selectOneCompanyInfo(entity);
        //修改 companyBaseInfo 的 是否登记字段为1
        CompanyBaseInfoEntity entity1 = new CompanyBaseInfoEntity();
        BeanUtils.copyProperties(companyBaseInfoVO,entity1);
        entity1.setCyRegistCd("1");
        companyBaseInfoService.updateCompanyBaseInfo(entity1);

        String bussId = IdUtil.getBussId();
        AcceptInfoEntity acceptInfoEntity = new AcceptInfoEntity();
        acceptInfoEntity.setAtId(IdUtil.getAceptId());
        acceptInfoEntity.setAtBussId(bussId);
        acceptInfoEntity.setAtAcptId(atAcptId);
        acceptInfoEntity.setAtAcpterNm(companyBaseInfoVO.getCyAcpterNm());
        acceptInfoEntity.setAtAcptDt(new Date());
        acceptInfoEntity.setAtApvlDt(new Date());
        acceptInfoEntity.setAtCompNm(companyBaseInfoVO.getCyNm());
        acceptInfoEntity.setAtCreditCode(companyBaseInfoVO.getCyCreditCode());
        acceptInfoEntity.setAtRegistId(companyBaseInfoVO.getCyRegistId());
        acceptInfoEntity.setAtCode(companyBaseInfoVO.getCyMgeCode());
        //受理状态 030000 登记；
        acceptInfoEntity.setAtAcptStd("030000");
        //档案类型
        acceptInfoEntity.setAtArchiveType(companyBaseInfoVO.getCyArchTpye());
        acceptInfoEntity.setAtAcptDt1(new Date());
        acceptInfoEntity.setStDt(new Date());
        acceptInfoEntity.setUtDt(new Date());
        acceptInfoEntity.setAtAresTolNum(companyBaseInfoVO.getCyAresTolNum());
        acceptInfoEntity.setAtMvCd(CodeUtil.MOVE_STD_0);
        acceptInfoService.insertAcceptInfo(acceptInfoEntity);

        //插入 company_info
        CompanyInfoEntity companyInfoEntity = new CompanyInfoEntity();
        companyInfoEntity.setCiId(IdUtil.getCompInfoId());
        companyInfoEntity.setCiBussId(bussId);
        companyInfoEntity.setCiNm(companyBaseInfoVO.getCyNm());
        companyInfoEntity.setCiRegistId(companyBaseInfoVO.getCyRegistId());
        companyInfoEntity.setCiCreditCode(companyBaseInfoVO.getCyCreditCode());
        companyInfoEntity.setCiRegistDeptNm(companyBaseInfoVO.getCyRegistDeptNm());
        companyInfoEntity.setCiRegistDeptCode(companyBaseInfoVO.getCyRegistDeptCode());
        companyInfoEntity.setCiMgeNm(companyBaseInfoVO.getCyMgeNm());
        companyInfoEntity.setCiMgeCode(companyBaseInfoVO.getCyMgeCode());
        companyInfoEntity.setCiRegistDt(companyBaseInfoVO.getCyRegistDt());
        companyInfoEntity.setCiOpenLimitDt(companyBaseInfoVO.getCyOpenLimitDt());
        companyInfoEntity.setCiBuessScope(companyBaseInfoVO.getCyBuessScope());
        companyInfoEntity.setCiOwner(companyBaseInfoVO.getCyOwner());
        companyInfoEntity.setCiType(companyBaseInfoVO.getCyType());
        companyInfoEntity.setCiTradeTpye(companyBaseInfoVO.getCyTradeTpye());
        companyInfoEntity.setCiBuessAds(companyBaseInfoVO.getCyBuessAds());
        companyInfoEntity.setCiRegistAmt(companyBaseInfoVO.getCyRegistAmt());
        companyInfoEntity.setCiPhone(companyBaseInfoVO.getCyPhone());
        companyInfoEntity.setCiArchTpye(companyBaseInfoVO.getCyArchTpye());
        companyInfoEntity.setCiBussType(companyBaseInfoVO.getCyBussType());
        companyInfoEntity.setCiPripalNm(companyBaseInfoVO.getCyPripalNm());
        companyInfoEntity.setCiCurrency(companyBaseInfoVO.getCyCurrency());
        companyInfoEntity.setCiAresTolNum(companyBaseInfoVO.getCyAresTolNum());
        companyInfoEntity.setCiRsrv1(companyBaseInfoVO.getCyRsrv1());
        companyInfoEntity.setCiCheckDt(companyBaseInfoVO.getCyCheckDt());
        //初始档案迁移状态 0 表示未做迁移或者迁移完成；1 表示迁出但未迁入
        companyInfoEntity.setCiMvCd(CodeUtil.MOVE_STD_0);
        companyInfoEntity.setCiBrowCd(CodeUtil.BROW_TYPE_STD_0);
        companyInfoEntity.setStDt(new Date());
        companyInfoEntity.setUtDt(new Date());
        companyInfoService.insertCompanyInfo(companyInfoEntity);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: java.util.List<com.cheng.data.entity.AcceptInfoEntity>
     * @date: 2021/9/22
     * @description: 根据条件，查询 acceptInfo 信息，并分页返回
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public RespPageInfoVO selectAcceptInfoList(ReqPageInfoVO pageVO) throws CommonException {

        //对查询结果进行分页
        Map<String,Object> map = new HashMap<>();
        map.put("srchReqFir",pageVO.getSrchReqFir());
        if (null != pageVO.getQryReqFir() && !"".equals(pageVO.getQryReqFir())) {
            map.put("qryReqFir", "%" + pageVO.getQryReqFir() + "%");
        }
        if(null != pageVO.getQryReqSeco() && !"".equals(pageVO.getQryReqSeco())){
            map.put("qryReqSeco",String.valueOf(pageVO.getQryReqSeco()));
        }
        //开始日期为当天的第一秒
        if(null != pageVO.getStDt() && !"".equals(pageVO.getStDt())) {
            map.put("stDt", pageVO.getStDt());
        }
        //截止日期为当天的最后一秒
        if(null != pageVO.getLmDt() && !"".equals(pageVO.getLmDt())) {
            map.put("lmDt", pageVO.getLmDt());
        }
        //初始化分页信息
        map.put("userCode",pageVO.getUserCode());
        PageInfo<AcceptInfoEntity> pageInfo = PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize()).doSelectPageInfo(() -> acceptInfoService.selectAcceptInfoList(map));
        List<AcceptInfoEntity> list = pageInfo.getList();
        List<AcceptInfoVO> acceptInfoVOList = new ArrayList<>();
        if (null != list && list.size() > 0){
            for (AcceptInfoEntity entity : list){
                AcceptInfoVO vo = new AcceptInfoVO();
                BeanUtils.copyProperties(entity,vo);
                //通过受理状态代码查询受理状态名称
                vo.setAtAcptStdNm(acptTypeInfOperation.queryAcptTypeInfNmByCode(vo.getAtAcptStd()));
                //将档案状态代码转化成档案状态名称
                vo.setAtArchType(archiveTypeInfOperation.queryArchiveTypeInfNmByCode(vo.getAtArchType()));
                acceptInfoVOList.add(vo);
            }
        }
        RespPageInfoVO respPageInfoVO = new RespPageInfoVO();
        respPageInfoVO.setList(acceptInfoVOList);
        respPageInfoVO.setPageNum(pageInfo.getPageNum());
        respPageInfoVO.setPageSize(pageInfo.getPageSize());
        respPageInfoVO.setPageCount(pageInfo.getTotal());
        return respPageInfoVO;
    }

    /**
     * @author: ChengJW
     * @params: [vo]
     * @return: int
     * @date: 2021/9/23
     * @description: 根据条件，修改 acceptInfo 信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAcceptInfo(AcceptInfoVO vo) throws CommonException {

        //先通过受理号或主键或业务申请编号，查询受理信息
        AcceptInfoEntity entity = new AcceptInfoEntity();
        BeanUtils.copyProperties(vo,entity);
        List<AcceptInfoEntity> list = acceptInfoService.selectAcceptInfo(entity);
        AcceptInfoEntity acceptInfoEntity = new AcceptInfoEntity();
        if (null == list || list.size() == 0){
            logger.info("未查询到受理信息");
            throw new CommonException("CJW202111010029","AcceptInfoOperationImpl","updateAcceptInfo");
        }
        acceptInfoEntity = list.get(0);
        //捆号
        if (StringUtil.isNotNull(vo.getAtBundleNo())) {
            acceptInfoEntity.setAtBundleNo(vo.getAtBundleNo());
        }
        //页数
        if (null != vo.getAtAresTolNum()){
            acceptInfoEntity.setAtAresTolNum(vo.getAtAresTolNum());
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            companyInfoVO.setCiBussId(vo.getAtBussId());
            companyInfoVO.setCiAresTolNum(vo.getAtAresTolNum());
            companyInfoService.updateCompanyInfo(companyInfoVO);
        }
        //受理状态
        if (StringUtil.isNotNull(vo.getAtAcptStd())){
            acceptInfoEntity.setAtAcptStd(vo.getAtAcptStd());
        }
        acceptInfoService.updateAcceptInfo(acceptInfoEntity);
    }

    /**
     * @author: ChengJW
     * @params: [vo]
     * @return: int
     * @date: 2021/9/23
     * @description: 根据业务受理编号，删除 accept_Info 信息，company_info 信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteAcceptInfo(AcceptInfoVO vo) throws CommonException {

        if (StringUtil.isNull(vo.getAtBussId())){
            logger.info("受理信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010055","AcceptInfoOperationImpl","deleteAcceptInfo");
        }
        AcceptInfoEntity entity = new AcceptInfoEntity();
        entity.setAtBussId(vo.getAtBussId());
        acceptInfoService.deleteAcceptInfo(entity);
        //删除 company_info 信息
        CompanyInfoEntity companyInfoEntity = new CompanyInfoEntity();
        companyInfoEntity.setCiBussId(vo.getAtBussId());
        companyInfoService.deleteCompanyInfo(companyInfoEntity);
    }

    /**
     * @author: ChengJW
     * @params: [vo]
     * @return: com.cheng.data.vo.AcceptInfoVO
     * @date: 2021/11/30
     * @description: 查找受理信息
     */
    @Override
    public AcceptInfoVO queryAcceptInfoVO(AcceptInfoVO vo) throws CommonException {

        if (null == vo){
            logger.error("受理信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010055","AcceptInfoOperationImpl","queryAcceptInfoVO");
        }
        AcceptInfoEntity entity = new AcceptInfoEntity();
        BeanUtils.copyProperties(vo,entity);
        List<AcceptInfoEntity> list = acceptInfoService.selectAcceptInfo(entity);
        AcceptInfoEntity acceptInfoEntity = new AcceptInfoEntity();
        if (null != list && list.size() > 0){
            acceptInfoEntity = list.get(0);
        }
        AcceptInfoVO acceptInfoVO = new AcceptInfoVO();
        BeanUtils.copyProperties(acceptInfoEntity,acceptInfoVO);
        acceptInfoVO.setAtAcptStdNm(acptTypeInfOperation.queryAcptTypeInfNmByCode(acceptInfoVO.getAtAcptStd()));
        return acceptInfoVO;
    }

    /**
    * @author: ChengJW
    * @params: [pageInfoVO]
    * @return: com.cheng.data.vo.RespPageInfoVO
    * @date: 2021/12/20
    * @description: 统计受理信息
    */
    @Override
    public RespPageInfoVO queryStatisticsAcceptList(ReqPageInfoVO pageVO) throws CommonException {
        if (pageVO == null || pageVO.getSrchReqFir() == null){
            logger.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyInfoOperationImpl","queryStatisticsVOList");
        }

        //对查询结果进行分页
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.STATISTIC_CODE,pageVO.getSrchReqFir());
        //开始日期为当天的第一秒
        if (StringUtil.isNotNull(pageVO.getStDt())){
            map.put("stDt",pageVO.getStDt()+" 00:00:00");
        }
        if (StringUtil.isNotNull(pageVO.getLmDt())){
            map.put("lmDt",pageVO.getLmDt()+" 23:59:59");
        }
        PageInfo<StatisticsVO> pageInfo = PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize()).doSelectPageInfo(() -> acceptInfoService.queryStatisticsAcceptList(map));
        List<StatisticsVO> list = new ArrayList<>();
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0){
            for (StatisticsVO vo : pageInfo.getList()){
                vo.setMgeNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(vo.getMgeCode()));
                list.add(vo);
            }
        }
        pageInfo.setList(list);
        return RespUtils.respPageInfoVO(pageInfo);
    }
}
